﻿package com.sos.ui
{ 
    import flash.display.MovieClip; 
	import flash.utils.Dictionary;
     
    public class GraveOverlayClip extends MovieClip 
    { 
	
		public var cardList:Dictionary = new Dictionary();
		public var cardCount:Number = 0;
		private var topRightCornerX:Number;
		private var topRightCornerY:Number;
		private var cardsXLine:Number;
		
		public function GraveOverlayClip() {
			//trace('INIT CARD HOLDER ' + this.name);
			
			topRightCornerX = -290;
			topRightCornerY = -150;
			cardsXLine = 10;
			
        } 
		
		public function addCard(ac:ActionCardClip):void {
			//trace("adding", ac.name, "to", this.name);
			if (!cardList[ac.instanceId]) {
				cardList[ac.instanceId] = ac;
				cardCount++;
				ac.graveOrder = cardCount;
			}
			
			this.addChild(ac);
			ac.x = getX(ac.graveOrder);
			ac.y = getY(ac.graveOrder);
			//trace(cardCount);
		}
		
		public function getX(cardOrder:Number):Number {
			var cardX:Number = topRightCornerX + (cardOrder - (getLine(cardOrder) - 1)*cardsXLine  - 1)*65;
			return cardX;
		}
		
		public function getY(cardOrder:Number):Number {
			var cardY:Number = topRightCornerY + (getLine(cardOrder) - 1)*75;
			return cardY;
		}
		
		public function getLine(cardOrder:Number):Number {
			
			var cardLine = Math.ceil(cardOrder/cardsXLine);
			//trace("CARD ", cardOrder, " LINE IS ", cardLine);
			return cardLine;
		}
		
		public function removeCard(cardId:String):void {
			//trace("Removing card", cardId, "from", this.name);
			this.removeChild(cardList[cardId]);
			for each (var ac:ActionCardClip in cardList) {
				if ((ac.instanceId != cardId) && (ac.graveOrder > cardList[cardId].graveOrder)) {
					
					ac.graveOrder--;
					ac.x = getX(ac.graveOrder);
					ac.y = getY(ac.graveOrder);
				}
			}
			//cardList.splice(cardId, 1);
			delete cardList[cardId];
			cardCount--;
			
		}
		
		
    } 
}